﻿/**  版本信息模板在安装目录下，可自行修改。
* UserInfo.cs
*
* 功 能： N/A
* 类 名： UserInfo
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2014/9/29 17:13:43   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using DBUtility;//Please add references
namespace BlogDH.DAL
{
    /// <summary>
    /// 数据访问类:UserInfo
    /// </summary>
    public partial class UserInfo
    {
        public UserInfo()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select max(userid) FROM UserInfo ");

            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from UserInfo");
            strSql.Append(" where username=@Username");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Username", OleDbType.VarChar,255)
			};
            parameters[0].Value = username;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BlogDH.Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into UserInfo(");
            strSql.Append("UserName,PWD,email,phone,lastloginTime,lastLoginIP,UpdateTme,CreateTime,sheng,shi,xian)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@PWD,@email,@phone,@lastloginTime,@lastLoginIP,@UpdateTme,@CreateTime,@Sheng,@Shi,@Xian)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@UserName", OleDbType.VarChar,255),
					new OleDbParameter("@PWD", OleDbType.VarChar,255),
					new OleDbParameter("@email", OleDbType.VarChar,255),
					new OleDbParameter("@phone", OleDbType.VarChar,255),
					new OleDbParameter("@lastloginTime", OleDbType.Date),
					new OleDbParameter("@lastLoginIP", OleDbType.VarChar,255),
					new OleDbParameter("@UpdateTme", OleDbType.Date),
					new OleDbParameter("@CreateTime", OleDbType.Date),
					new OleDbParameter("@Sheng", OleDbType.Integer,4),
					new OleDbParameter("@Shi", OleDbType.Integer,4),
					new OleDbParameter("@Xian", OleDbType.Integer,4)};
            parameters[0].Value = CommMethod.HtmlData(model.UserName);
            parameters[1].Value = model.PWD;
            parameters[2].Value = CommMethod.HtmlData(model.email);
            parameters[3].Value = CommMethod.HtmlData(model.phone);
            parameters[4].Value = model.lastloginTime;
            parameters[5].Value = model.lastLoginIP;
            parameters[6].Value = System.DateTime.Now;
            parameters[7].Value = System.DateTime.Now;
            parameters[8].Value = model.Sheng;
            parameters[9].Value = model.Shi;
            parameters[10].Value = model.Xian;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BlogDH.Model.UserInfo model)
        {
            System.Collections.Generic.List<OleDbParameter> list = new System.Collections.Generic.List<OleDbParameter>();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update UserInfo set ");

            if (!string.IsNullOrEmpty(model.PWD))
            {
                strSql.Append("PWD=@PWD,");
                list.Add(new OleDbParameter("@PWD", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = model.PWD;
            }
            if (!string.IsNullOrEmpty(model.email))
            {
                strSql.Append("email=@email,");
                list.Add(new OleDbParameter("@email", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = CommMethod.HtmlData(model.email);
            }
            if (!string.IsNullOrEmpty(model.phone))
            {
                strSql.Append("phone=@phone,");
                list.Add(new OleDbParameter("@phone", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = CommMethod.HtmlData(model.phone);
            }
            if (model.lastloginTime != null)
            {
                strSql.Append("lastloginTime=@lastloginTime,");
                list.Add(new OleDbParameter("@lastloginTime", OleDbType.Date));
                list[list.Count - 1].Value = model.lastloginTime;
            }
            if (!string.IsNullOrEmpty(model.lastLoginIP))
            {
                strSql.Append("lastLoginIP=@lastLoginIP,");
                list.Add(new OleDbParameter("@lastLoginIP", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = model.lastLoginIP;
            }
            if (model.Sheng != null && model.Sheng != 0)
            {
                strSql.Append("sheng=@sheng,");
                list.Add(new OleDbParameter("@sheng", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Sheng;
            }
            if (model.Shi != null && model.Shi != 0)
            {
                strSql.Append("Shi=@Shi,");
                list.Add(new OleDbParameter("@Shi", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Shi;
            }
            if (model.Xian != null && model.Xian != 0)
            {
                strSql.Append("Xian=@Xian,");
                list.Add(new OleDbParameter("@Xian", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Xian;
            }

            strSql.Append("UpdateTme=@UpdateTme");
            list.Add(new OleDbParameter("@UpdateTme", OleDbType.Date));
            list[list.Count - 1].Value = System.DateTime.Now;

            strSql.Append(" where UserID=@UserID and UserName=@UserName");
            list.Add(new OleDbParameter("@UserID", OleDbType.Integer, 4));
            list[list.Count - 1].Value = model.UserID;
            list.Add(new OleDbParameter("@UserName", OleDbType.VarChar, 255));
            list[list.Count - 1].Value = model.UserName;

            OleDbParameter[] parameters = new OleDbParameter[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                parameters[i] = list[i];
            }

            //OleDbParameter[] parameters = {
            //        new OleDbParameter("@UserName", OleDbType.VarChar,255),
            //        new OleDbParameter("@PWD", OleDbType.VarChar,255),
            //        new OleDbParameter("@email", OleDbType.VarChar,255),
            //        new OleDbParameter("@phone", OleDbType.VarChar,255),
            //        new OleDbParameter("@lastloginTime", OleDbType.Date),
            //        new OleDbParameter("@lastLoginIP", OleDbType.VarChar,255),
            //        new OleDbParameter("@UpdateTme", OleDbType.Date),
            //        new OleDbParameter("@CreateTime", OleDbType.Date),
            //        new OleDbParameter("@UserID", OleDbType.Integer,4)};
            //parameters[0].Value = model.UserName;
            //parameters[1].Value = model.PWD;
            //parameters[2].Value = model.email;
            //parameters[3].Value = model.phone;
            //parameters[4].Value = model.lastloginTime;
            //parameters[5].Value = model.lastLoginIP;
            //parameters[6].Value = model.UpdateTme;
            //parameters[7].Value = model.CreateTime;
            //parameters[8].Value = model.UserID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserInfo ");
            strSql.Append(" where UserID=@UserID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@UserID", OleDbType.Integer,4)
			};
            parameters[0].Value = UserID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UserIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserInfo ");
            strSql.Append(" where UserID in (" + UserIDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.UserInfo GetModel(int UserID, string username)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserID,UserName,PWD,email,phone,sheng,shi,xian,lastloginTime,lastLoginIP,UpdateTme,CreateTime from UserInfo ");
            strSql.Append(" where UserID=@UserID and Username=@username");
            OleDbParameter[] parameters = {
					new OleDbParameter("@UserID", OleDbType.Integer,4),
					new OleDbParameter("@UserName", OleDbType.VarChar,255)
			};
            parameters[0].Value = UserID;
            parameters[1].Value = username;

            BlogDH.Model.UserInfo model = new BlogDH.Model.UserInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.UserInfo GetModel(string UserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserID,UserName,PWD,email,phone,sheng,shi,xian,lastloginTime,lastLoginIP,UpdateTme,CreateTime from UserInfo ");
            strSql.Append(" where UserName=@UserName");
            OleDbParameter[] parameters = {
					new OleDbParameter("@UserName", OleDbType.VarChar,255)
			};
            parameters[0].Value = UserName;

            BlogDH.Model.UserInfo model = new BlogDH.Model.UserInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.UserInfo DataRowToModel(DataRow row)
        {
            BlogDH.Model.UserInfo model = new BlogDH.Model.UserInfo();
            if (row != null)
            {
                if (row["UserID"] != null && row["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(row["UserID"].ToString());
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["PWD"] != null)
                {
                    model.PWD = row["PWD"].ToString();
                }
                if (row["email"] != null)
                {
                    model.email = row["email"].ToString();
                }
                if (row["phone"] != null)
                {
                    model.phone = row["phone"].ToString();
                }
                if (row["sheng"] != null && row["sheng"].ToString() != "")
                {
                    model.Sheng = int.Parse(row["sheng"].ToString());
                }
                if (row["shi"] != null && row["shi"].ToString() != "")
                {
                    model.Shi = int.Parse(row["shi"].ToString());
                }
                if (row["xian"] != null && row["xian"].ToString() != "")
                {
                    model.Xian = int.Parse(row["xian"].ToString());
                }
                if (row["lastloginTime"] != null && row["lastloginTime"].ToString() != "")
                {
                    model.lastloginTime = DateTime.Parse(row["lastloginTime"].ToString());
                }
                if (row["lastLoginIP"] != null)
                {
                    model.lastLoginIP = row["lastLoginIP"].ToString();
                }
                if (row["UpdateTme"] != null && row["UpdateTme"].ToString() != "")
                {
                    model.UpdateTme = DateTime.Parse(row["UpdateTme"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserID,UserName,PWD,email,phone,sheng,shi,xian,lastloginTime,lastLoginIP,UpdateTme,CreateTime ");
            strSql.Append(" FROM UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.UserID desc");
            }
            strSql.Append(")AS Row, T.*  from UserInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "UserInfo";
            parameters[1].Value = "UserID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

